// VPolyline.cpp
//
#include "VPolyLine.h"

#include "OpenGL/Renderer.h"
#include "OpenGL/Selection.h"
#include "OpenGL/OpenGL.h"

#include "Intersection.h"

// ------------------------------------------------------------------------------------------------
VPolyLine::VPolyLine()
{}

// ------------------------------------------------------------------------------------------------
VPolyLine::VPolyLine( const std::vector<OpenGL::Math::dvec3> &pts )
{
	_points = pts;

	for( size_t i=0; i<_points.size(); ++i )
		_boundingBox.extend( _points[i] );
}

// ------------------------------------------------------------------------------------------------
VObject::ObjectType VPolyLine::type() const		{ return PolyLine; }

// ------------------------------------------------------------------------------------------------
std::vector<OpenGL::Math::dvec3> VPolyLine::snapPoints() const
{
	std::vector<OpenGL::Math::dvec3> pts;
	for( size_t i=0; i<_points.size(); ++i )
		pts.push_back( _points[i] );

	return pts;
}

// ------------------------------------------------------------------------------------------------
OpenGL::Math::dvec3 VPolyLine::textPosition() const
{
	return _points[ _points.size()/2];
}

// ------------------------------------------------------------------------------------------------
void VPolyLine::draw( OpenGL::Renderer *r ) const
{
	OpenGL::Math::dvec3 o = r->origin();

	//glPushAttrib(GL_COLOR_BUFFER_BIT);
		//glLineWidth(2.f + (isHilighted() * .5f) );
		//glColor3ub(0,0,0);
		//glBegin(GL_LINES);
		//	glVertex2dv( OpenGL::Math::value_ptr(_p0-o) );
		//	glVertex2dv( OpenGL::Math::value_ptr(_p1-o) );
		//glEnd();

		glColor3fv( color().rgba() );
		glLineWidth(2.f);
		glBegin(GL_LINE_STRIP);
			for( size_t i=0; i<_points.size(); ++i )
				glVertex2dv( OpenGL::Math::value_ptr(_points[i]-o) );
		glEnd();
	//glPopAttrib();	
}

// ------------------------------------------------------------------------------------------------
void VPolyLine::drawForSelection( OpenGL::Renderer *r ) const
{
	OpenGL::Selection::Name name( _id );

	OpenGL::Math::dvec3 o = r->origin();

	glLineWidth(2.f);
	glBegin(GL_LINE_STRIP);
		for( size_t i=0; i<_points.size(); ++i )
			glVertex2dv( OpenGL::Math::value_ptr(_points[i]-o) );
	glEnd();
}

// ------------------------------------------------------------------------------------------------
bool VPolyLine::intersects( const OpenGL::Math::box3 &b ) const 
{
	// test p0-p1 against box edges
	bool intersect = false;

	for( size_t i=0; i<_points.size()-1; ++i )
	{
		const OpenGL::Math::dvec2 p0( _points[i+0].x,_points[i+0].y );
		const OpenGL::Math::dvec2 p1( _points[i+1].x,_points[i+1].y );

		intersect |= lineIntersection( p0,p1, OpenGL::Math::dvec2(b.minPoint().x,b.minPoint().y), OpenGL::Math::dvec2(b.minPoint().x,b.maxPoint().y) ); // left
		intersect |= lineIntersection( p0,p1, OpenGL::Math::dvec2(b.minPoint().x,b.maxPoint().y), OpenGL::Math::dvec2(b.maxPoint().x,b.maxPoint().y) ); // top
		intersect |= lineIntersection( p0,p1, OpenGL::Math::dvec2(b.maxPoint().x,b.maxPoint().y), OpenGL::Math::dvec2(b.maxPoint().x,b.minPoint().y) ); // right
		intersect |= lineIntersection( p0,p1, OpenGL::Math::dvec2(b.maxPoint().x,b.minPoint().y), OpenGL::Math::dvec2(b.minPoint().x,b.minPoint().y) ); // bottom

		if( intersect )
			break;
	}

	return intersect;
}
